﻿using System.Collections.Generic;
using System.IO;
using System.Linq;
using NPOI.HSSF.UserModel;
using NPOI.SS.UserModel;
using Resn.WhuPub.BLL;
using Resn.WhuPub.DAL;
using Resn.WhuPub.DML;
using System;

namespace Resn.WhuPub.BLL
{
        /// <summary>
        /// 实现书籍的属性和方法
        /// </summary>
        public class Book : Entity<BookModel>
        {
                #region 构造函数

                /// <summary>
                /// 创建书籍实例
                /// </summary>
                /// <param name="model">书籍模型</param>
                public Book(BookModel model)
                        : base(model)
                {
                }

                #endregion 构造函数

                #region 属性

                #region 基本属性

                
                

                /// <summary>
                /// 获取联系人标识
                /// </summary>
                /// <value>
                /// 联系人标识
                /// </value>
                public string ContactId
                {
                        get
                        {
                                return Model.ContactId;
                        }
                        
                }

                /// <summary>
                /// 获取或设置名称
                /// </summary>
                /// <value>
                /// 名称
                /// </value>
                public string Name
                {
                        get
                        {
                                return Model.Name;
                        }
                        set
                        {
                                Model.Name = value;
                        }
                }

                /// <summary>
                /// 获取或设置参与编写方式
                /// </summary>
                /// <value>
                /// 参与编写方式
                /// </value>
                public PartakeType PartakeType
                {
                        get
                        {
                                return (PartakeType)Enum.Parse(typeof(PartakeType), Model.PartakeType);
                        }
                        set
                        {
                                Model.PartakeType = value.ToString();
                        }
                }

                /// <summary>
                /// 获取或设置出版社
                /// </summary>
                /// <value>
                /// 出版社
                /// </value>
                public string Publisher
                {
                        get
                        {
                                return Model.Publisher;
                        }
                        set
                        {
                                Model.Publisher = value;
                        }
                }

                #endregion 基本属性

                

                /// <summary>
                /// 获取联系人
                /// </summary>
                /// <value>
                /// 联系人
                /// </value>
                public Contact Contact
                {
                        get
                        {
                                var model = DBStorage.Instance.Select<ContactModel>(ContactId);
                                return new Contact(model);
                        }
                }

                #endregion 属性

                #region 方法

                /// <summary>
                /// 获取指定标识的对象
                /// </summary>
                /// <param name="id">标识</param>
                /// <returns></returns>
                public static Book Select(string id)
                {
                        return Select<Book>(id);
                }

                /// <summary>
                /// 创建书籍
                /// </summary>
                /// <returns>
                /// 创建的书籍对象
                /// </returns>
                public Book CreateBook()
                {
                        var model = new BookModel
                        {
                                Id = Guid.NewGuid().ToString(),
                        };

                        return new Book(model);
                }




                /// <summary>
                /// 从EXCEL文件加载书籍集合
                /// </summary>
                /// <param name="path">EXCEL文件路径</param>
                /// <returns>书籍</returns>
                public Dictionary<Contact, List<Book>> LoadBooksFromExcel(string path)
                {
                        string[] r = Enum.GetNames(typeof(CategoryLevel));

                        if (!File.Exists(path))
                        {
                                throw new InvalidOperationException("文件不存在，无法加载");
                        }

                        var result = new Dictionary<Contact, List<Book>>();

                        using (var fileStream = new FileStream(path, FileMode.Open))
                        {
                                var workBook = new HSSFWorkbook(fileStream);
                                var sheet = workBook.GetSheetAt(0);

                                var rowsCount = sheet.PhysicalNumberOfRows;
                                var columnsCount = sheet.GetRow(0).PhysicalNumberOfCells;

                                if (columnsCount < 15)
                                {
                                        throw new InvalidOperationException("列错误，无法导入");
                                }

                                if (rowsCount < 2)
                                {
                                        throw new InvalidOperationException("行错误，无法导入");
                                }

                                for (int i = 1; i < rowsCount; i++)
                                {
                                        IRow row = sheet.GetRow(i);

                                        string bookName = row.Cells[0].StringCellValue;
                                        string seriesName = row.Cells[1].StringCellValue;
                                        string versionType = row.Cells[2].StringCellValue;

                                        string versionTimes = row.Cells[3].StringCellValue;
                                        string printTimes = row.Cells[4].StringCellValue;
                                        string format = row.Cells[5].StringCellValue;

                                        string priceString = row.Cells[6].StringCellValue;
                                        string contactName = row.Cells[7].StringCellValue;
                                        string isbn = row.Cells[8].StringCellValue;

                                        string chargeEditor = row.Cells[9].StringCellValue;
                                        string editorialOffice = row.Cells[10].StringCellValue;
                                        string planningEditor = row.Cells[11].StringCellValue;

                                        string classify = row.Cells[12].StringCellValue;
                                        string createTime = row.Cells[13].StringCellValue;
                                        string contentAbstract = row.Cells[14].StringCellValue;

                                        double price;
                                        bool success = Double.TryParse(priceString, out price);
                                        if (!success)
                                        {
                                                throw new InvalidOperationException(i + "行定价错误，无法导入");
                                        }

                                        throw new NotImplementedException("未完成");

                                        //Contact contact;
                                        //if (result.Keys.Any(j => j.Name == contactName))
                                        //{
                                        //        contact = result.Keys.Single(k => k.Name == contactName);
                                        //}
                                        //else
                                        //{
                                        //        contact = ContactManager.Instance.CreateContact(string.Empty,
                                        //                                                        contactName);
                                        //        result.Add(contact, new List<Book>());
                                        //}

                                        //Book book;
                                        //if (result[contact].Keys.Any(j => j.Name == bookName))
                                        //{
                                        //        book = result[contact].Keys.Single(k => k.Name == bookName);
                                        //}
                                        //else
                                        //{
                                        //        book = contact.CreateBook();
                                        //        result[contact].Add(book);
                                        //}
                                }
                        }

                        return result;
                }

                #endregion

        }
}